package com.zy.baselib.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Choreographer;

import androidx.appcompat.widget.AppCompatTextView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * @author: shalei
 * @since: 2020-02-10 15:23
 * @Desc:
 **/
public class MarqueeTextView extends AppCompatTextView {
    private static final byte MARQUEE_STOPPED = 0x0;
    private static final byte MARQUEE_STARTING = 0x1;
    private static final byte MARQUEE_RUNNING = 0x2;
    /**
     * 滚动次数
     */
    private int marqueeNum = 3;//-1为永久循环，大于0是循环次数。
    private OnMarqueeListener onMarqueeListener;

    public void setMarqueeNum(int marqueeNum) {
        this.marqueeNum = marqueeNum;
    }

    public MarqueeTextView(Context context) {
        super(context);
        setAttr();
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setAttr();
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setAttr();
    }

    /**
     * 始终获取焦点
     * 跑马灯在TextView处于焦点状态的时候才会滚动
     */
    @Override
    public boolean isFocused() {
        return true;
    }

    /**
     * 设置相关属性
     */
    private void setAttr() {
        this.setEllipsize(TextUtils.TruncateAt.MARQUEE);//设置跑马等效果
        this.setMarqueeRepeatLimit(marqueeNum);//设置跑马灯重复次数
        this.setSingleLine(true);//设置单行
    }

    @SuppressLint("CheckResult")
    public void setMarqueeListener(OnMarqueeListener onMarqueeListener) {
        this.onMarqueeListener = onMarqueeListener;
    }

    private Choreographer.FrameCallback mRestartCallback = new Choreographer.FrameCallback() {
        @Override
        public void doFrame(long frameTimeNanos) {

            try {
                Field marquee = MarqueeTextView.this.getClass().getSuperclass().getDeclaredField("mMarquee");
                if(marquee != null) {
                    marquee.setAccessible(true);
                    Object obj = marquee.get(MarqueeTextView.this);
                    if(obj != null) {
                        Class cls = obj.getClass();
                        Field field = cls.getDeclaredField("mStatus");
                        if(field != null) {
                            field.setAccessible(true);
                            byte mStatus = ((Byte)field.get(obj)).byteValue();
                            if(mStatus == MARQUEE_RUNNING) {
                                Field field1 = cls.getDeclaredField("mRepeatLimit");
                                field1.setAccessible(true);
                                int mRepeatLimit = ((Integer)field1.get(obj)).intValue();;
                                if(mRepeatLimit >= 0) {
                                    mRepeatLimit--;
                                }
                                if(onMarqueeListener != null) {
                                    onMarqueeListener.onMarqueeRepeateChanged(mRepeatLimit);
                                }
                                Method method = cls.getDeclaredMethod("start", Integer.TYPE);
                                method.setAccessible(true);
                                method.invoke(obj, mRepeatLimit);
                            }
                        }
                    }
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    //状态监听接口
    public interface OnMarqueeListener {
        void onMarqueeRepeateChanged(int repeatLimit);
    }

}